使用安卓的设备屏幕大小的java拖放图像
我正在做一个项目,在整个屏幕上移动浮动按钮。我已经为drag&;编写了一个简单的代码;放置浮动按钮的图像。但它并没有在设备的特定屏幕中移动。这是隐藏在标题,也削减了所有的角落。我附上了一些截图。我怎样才能从四面八方添加一些填充物。
这是我的拖放代码;丢弃事件:
mFloatingActionButton.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
Intent mIntent = new Intent(mActivity, Cart.class);
startActivity(mIntent);
} else {
int X = (int) event.getRawX();
int Y = (int) event.getRawY();
RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
if (X > windowwidth) {
X = windowwidth;
}
if (Y > windowheight) {
Y = windowheight;
}
lParams.leftMargin = X - _xDelta;
lParams.topMargin = Y - _yDelta;
lParams.rightMargin = 100;
lParams.bottomMargin = 100;
view.setLayoutParams(lParams);
break;
}
mRrootLayout.invalidate();
return true;
}
return true;
}
});
xml文件:
<FrameLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:fab="http://schemas.安卓.com/apk/res-auto"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="@color/white_color">
<in.srain.cube.views.GridViewWithHeaderAndFooter
安卓:id="@+id/gridProductList"
安卓:layout_width="fill_parent"
安卓:layout_height="fill_parent"
安卓:fadingEdge="none"
安卓:focusable="false"
安卓:listSelector="@安卓:color/transparent"
安卓:numColumns="2"
安卓:overScrollMode="never"
安卓:paddingBottom="4dp"
安卓:scrollbarStyle="outsideOverlay"
安卓:scrollbars="none"
安卓:stretchMode="columnWidth" />
<RelativeLayout
安卓:id="@+id/rl_parent_floating"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:visibility="visible">
<RelativeLayout xmlns:fab="http://schemas.安卓.com/apk/res-auto"
安卓:id="@+id/rl_floating_button"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<com.melnykov.fab.FloatingActionButton
安卓:id="@+id/fab"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:layout_marginTop="3dp"
安卓:src="@drawable/ic_floating_cart"
fab:fab_colorNormal="@color/header_color_red"
fab:fab_colorPressed="@color/colorPrimaryDark"
fab:fab_colorRipple="@color/gold_color" />
<TextView
安卓:id="@+id/txtCartCount"
安卓:layout_width="30dp"
安卓:layout_height="30dp"
安卓:layout_alignRight="@+id/fab"
安卓:layout_alignTop="@+id/fab"
安卓:background="@drawable/cart_gold_circle"
安卓:gravity="center"
安卓:singleLine="true"
安卓:text="2"
安卓:textColor="@安卓:color/white"
安卓:textSize="8sp" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
# 1 楼答案
经过大量的研究,我终于找到了这个可怕的回购协议的解决方案。使用此库:) Floating button in github